package cn.com.main;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class FileSearchTool {

    static ThreadPoolExecutor executorService = new ThreadPoolExecutor(30, 500, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100000000));

    static Scanner scanner = new Scanner(System.in);

    static Set<String> matchedFiles = new ConcurrentSkipListSet<>();


    public static void main(String[] args) {
        while (true) {

            System.out.print("\n请输入要搜索的关键字: ");
            String keyword = scanner.nextLine();
            List<CompletableFuture<Void>> futures = searchFiles(new File("E:\\"), keyword);
            //matchedFiles.addAll(searchFiles(new File("c:/"), keyword));
            //matchedFiles.addAll(searchFiles(new File("d:/"), keyword));

            // 等待所有 CompletableFuture 完成
            CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

            // 等待所有任务完成
            try {
                allOf.get();
            } catch (InterruptedException | ExecutionException ignored) {
            }


            if (matchedFiles.isEmpty()) {
                System.out.println("未找到匹配的文件。");
            } else {
                System.out.println("匹配到的文件:");
                for (String filePath : matchedFiles) {
                    System.out.println(filePath);
                }
                matchedFiles.clear();
            }
            System.out.println();
        }
    }

    private static List<CompletableFuture<Void>> searchFiles(File directory, String keyword) {


        List<CompletableFuture<Void>> futures = new CopyOnWriteArrayList<>();
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                List<CompletableFuture<Void>> finalFutures = futures;
                futures.addAll(Arrays.stream(files).map(file -> CompletableFuture.runAsync(() -> {
                            if (file.isDirectory()) {
                                // 使用线程池并行搜索子目录
                                finalFutures.addAll(searchFiles(file, keyword));
                            } else {
                                // 判断文件名是否匹配关键字
                                if (matchesKeyword(file.getName(), keyword)) {
                                    matchedFiles.add(file.getAbsolutePath());
                                }
                            }
                        }, executorService))
                        .collect(Collectors.toList()));
            }
        }

        return futures;
    }


    private static boolean matchesKeyword(String fileName, String keyword) {
        if (fileName == null || keyword == null) {
            return false;
        }
        return fileName.equals(keyword);
    }

}
